package org.ucas.kgraph.dto;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Defaults;
import org.springframework.http.HttpStatus;
import org.springframework.ui.context.Theme;
import org.ucas.kgraph.entity.Term;
import org.ucas.kgraph.utils.exception.NotFoundException;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 词条的Dto对象
 * @author suemi
 */
public class TermDto  {

  public Long id;

  public String definition;

  public String description;
  
  public String direction;
  
  public String theme;
  
  public Float cost;
  
  public Long rootId;

  public Long documentId;

  public List<String> tagList;

  public Integer level;

  public static TermDto of(Term term){
    if(term==null) throw NotFoundException.Term_NotExists;
    TermDto termDto = new TermDto();
    termDto.id = term.getId();
    termDto.definition = term.getDefinition();
    termDto.description = term.getDescription();
    termDto.rootId = term.getRootId();
    termDto.documentId = term.getDocumentId();
    termDto.tagList = term.getTagList();
    termDto.cost=term.getCost();
    termDto.direction=term.getDirection();
    termDto.theme=term.getTheme();
    return termDto;
  }

  public static TermDto from(HttpServletRequest request){
    TermDto termDto = new TermDto();
    termDto.id = request.getParameter("id")==null?null:Long.valueOf(request.getParameter("id"));
    termDto.definition = request.getParameter("definition");
    termDto.description = request.getParameter("description");
    termDto.rootId = request.getParameter("rootId")==null?null:Long.valueOf(request.getParameter("rootId"));
   
    termDto.documentId = request.getParameter("documentId")==null?null:Long.valueOf(request.getParameter("documentId"));
    termDto.cost= request.getParameter("cost")==null?null:Float.valueOf(request.getParameter("cost"));
    termDto.direction = request.getParameter("direction")==null?null:request.getParameter("direction");
    String[] tagList = request.getParameterValues("tagList[]");
    if(tagList!=null) termDto.tagList = Arrays.asList(tagList);
    return termDto;
  }

  /**
   * 更新Term,但核心信息如定义和Id及RootId只允许Term中为空也就是创建时才可以写入
   * level等属性只允许计算得到,不允许手动写入
   * 其他属性只有非空时才会写入
   * @param term
   * @return
   */
  public Term writeTo(Term term){

    if(term==null) throw NotFoundException.Term_NotExists;

    //仅当term中Id和rootId为空时才允许改动
    if(term.getId()==null) term.setId(this.id);
    if(term.getRootId()==null) term.setRootId(rootId);
    if(term.getDefinition()==null) term.setDefinition(definition);
    
    if(direction!=null) term.setDirection(direction);
    
    if(description!=null) term.setDescription(description);
    if(documentId!=null && !documentId.equals(Defaults.defaultValue(Long.TYPE)))
      term.setDocumentId(documentId);
    if(tagList!=null) term.setTagList(tagList);
    if(cost!=null) term.setCost(cost);
    return term;
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getDefinition() {
    return definition;
  }

  public void setDefinition(String definition) {
    this.definition = definition;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public Long getRootId() {
    return rootId;
  }

  public void setRootId(Long rootId) {
    this.rootId = rootId;
  }

  public Long getDocumentId() {
    return documentId;
  }

  public void setDocumentId(Long documentId) {
    this.documentId = documentId;
  }

  public List<String> getTagList() {
    return tagList;
  }

  public void setTagList(List<String> tagList) {
    this.tagList = tagList;
  }

  public Integer getLevel() {
    return level;
  }

  public void setLevel(Integer level) {
    this.level = level;
  }

  public Float getCost() {
	return cost;
  }

  public void setCost(Float cost) {
	this.cost = cost;
  }

  public String getDirection() {
	return direction;
  }

  public void setDirection(String direction) {
	this.direction = direction;
  }
  
}
